#pragma once
#include "protocol.h"
#include "protocol.h"
#include <memory>
using namespace protocol;
using namespace protocol;

namespace calculater
{
    class Cal
    {
    public:
        Cal()
        {
        }
        Cal(shared_ptr<Request> req)
            : _x(req->GetX()), _y(req->GetY()), _oper(req->GetOper())
        {
        }

        shared_ptr<Response> calculate()
        {
            switch (_oper)
            {
            case '+':
                _result = _x + _y;
                break;
            case '-':
                _result = _x - _y;
                break;
            case '*':
                _result = _x * _y;
                break;
            case '/':
            {
                if (_y == 0)
                    _retinfo = "除数为0,结果无意义";
                else
                    _result = _x / _y;
            }
            break;
            case '%':
            {
                if (_y == 0)
                    _retinfo = "模0,结果未定义";
                else
                    _result = _x % _y;
            }
            break;

            default:
                _retinfo = "结果无效,未录入该运算符";
                break;
            }
            shared_ptr<Response> resp = Factor::BuildResponse(_result, _retinfo);
            return resp;
        }

        shared_ptr<Response> operator()() // 构建一个仿函数
        {
            return calculate();
        }

        string Answer()
        {
            return "result = " + to_string(_result) + " ret_info = " + _retinfo;
        }

    private:
        int _x;
        int _y;
        char _oper;
        int _result;
        string _retinfo = "结果无误";
    };
};